<?php

namespace app\index\controller;

use think\facade\View;

class WeatherCity
{

    public function index()
    {
        $keyword = get_param('keyword');
        View::assign('keyword', $keyword);
        return View::fetch();
    }

    public function getList()
    {
        //接受参数
        $page = get_param('page');
        $limit = get_param('limit');
        $keyword = get_param('keyword');
        $weatherCityModel = new \app\common\model\WeatherCity();
        //构建查询条件
        $condition = array();
        if (!empty($keyword)) $condition[] = ['cityZh|provinceZh|leaderZh', 'like', '%' . $keyword . '%'];
        $data = $weatherCityModel->where($condition)->page($page)->limit($limit)->select();
        $count = $weatherCityModel->where($condition)->count();
        output_data_table($data, $count);
    }

    public function add()
    {
        $mobile = post_param('mobile');
        $password = post_param('password');
        $name = post_param('name');
        $age = post_param('age');
        $filename = post_param('filename');
        $captcha = post_param('captcha');//接受验证码
        if (empty($mobile)) return View::fetch();
        $memberModel = new \app\common\model\Member();
        //验证码
        if (!captcha_check($captcha)) output_error('验证码无效');
        //数据校验
        $data = $memberModel->where('member_mobile', '=', $mobile)->find();
        if (!empty($data)) output_error('此号码已存在~');
        //插入数据
        $data = array();
        $data['member_mobile'] = $mobile;
        $data['member_password'] = md5_encrypt($mobile, $password);
        $data['member_avatar'] = $filename;
        $data['member_name'] = $name;
        $data['member_age'] = $age;
        $data['add_time'] = time();
        $memberModel->insert($data);
        output_success();
        return null;
    }

    public function edit()
    {
        $mobile = post_param('mobile');
        $name = post_param('name');
        $age = post_param('age');
        $filename = post_param('filename');
        $memberModel = new \app\common\model\Member();
        if (empty($mobile) || empty($name) || empty($age)) {
            $id = get_param('id');
            if (empty($id)) output_error_param();
            $data = $memberModel->where('member_id', '=', $id)->find();
            if (empty ($data)) output_error_param();
            View::assign('data', $data);
            return View::fetch();
        }
        //提交后的处理
        $id = get_param('id');
        $data = $memberModel->where('member_mobile', '=', $mobile)->find();
        if (!empty($data) && $data['member_id'] != $id) output_error('此号码已存在~');
        //构建更新数据
        $update = array();
        $update['member_mobile'] = $mobile;
        $update['member_name'] = $name;
        $update['member_age'] = $age;
        if (!empty($filename)) $update['member_avatar'] = $filename;
        //执行更新语句
        $memberModel->where('member_id', '=', $id)->update($update);
        output_success();
        return null;
    }

    public function modify()
    {
        //渲染页面
        $id = get_param('id');
        $id = substr($id, 0, strlen($id) - 1);
        $name = post_param('name');
        $value = post_param('value');
        if (empty($name) || empty($value)) {
            View::assign('id', $id);
            return View::fetch();
        }
        $id = explode(',', $id);
        $memberModel = new \app\common\model\Member();
        $update = array();
        $update[$name] = $value;
        $memberModel->where("member_id", 'in', $id)->update($update);
        output_success();
        return null;
    }

    public function batch()
    {
        //完善接口，实现批量删除
        $id = get_param('id');
        $id = substr($id, 0, strlen($id) - 1);
        $id = explode(',', $id);
        $memberModel = new \app\common\model\Member();
        $memberModel->where("member_id", 'in', $id)->delete();
        output_success();
    }

    public function delete()
    {
        $id = get_param('id');
        $memberModel = new \app\common\model\Member();
        $memberModel->where('member_id', '=', $id)->delete();
        output_success();
    }

}
